from rest_framework.response import Response
from rest_framework.views import APIView

from UserApp.models import *
from my_django.ser import *

# 奇数判断
class AccessAddAPI(APIView):
    def get(self,request):
        uid = request.GET.get('uid')
        print(uid)
        if int(uid) & 1:
            return Response({"msg":'为奇数用户,可以访问','code':200})
        else:
            return Response({"msg":'抱歉,您没有权限访问改页面','code':400})

# 偶数判断
class AccessEvenAPI(APIView):
    def get(self,request):
        uid = request.GET.get('uid')
        a = int(uid) & 1
        if a == 0:
            return Response({"msg":'为偶数用户,可以访问','code':200})
        else:
            return Response({"msg":'抱歉,您没有权限访问改页面','code':400})


# 二进制权限判断
class Binary(APIView):
    def get(self,request):
        level = request.GET.get('level')
        key1 = eval(level) & 0b001
        key2 = eval(level) & 0b010
        key3 = eval(level) & 0b100
        if key1 != 0:
            return Response({"msg":'可以访问','code':200})

        if key2 != 0:
            return Response({"msg":'可以访问','code':200})

        if key3 != 0:
            return Response({"msg":'可以访问','code':200})

        else:
            return Response({"msg":'抱歉,您没有权限访问改页面','code':400})



# 权限管理
class Access(APIView):
    def get(self,request):
        queryset = User.objects.all()
        ser = UserSer(queryset,many=True)
        return Response(ser.data)

    def put(self,request):
        uid = request.GET.get('uid')
        level = request.data.get('level')

        User.objects.filter(id = uid).update(level = level)
        return Response({"msg":'修改成功','code':200})


def xTree(datas):
    lists = []  # 定义空列表
    tree = {}  # 定义空字典
    for i in datas:
        item = i
        tree[item['id']] = item

    for i in datas:
        obj = i
        if not obj['pid']:
            root = tree[obj['id']]
            lists.append(root)
        else:
            parent_id = obj['pid']
            if 'children' not in tree[parent_id]:
                tree[parent_id]['children'] = []
            tree[parent_id]['children'].append(tree[obj['id']])
    return lists




class WorkOrderView(APIView):
    # 添加工单分类信息
    def post(self, request):
        name = request.data.get('name')
        pid = request.data.get('pid')
        print(pid,'11111111111111111111111')
        if pid:
            Cate.objects.create(name=name, pid=pid)
        else:
            Cate.objects.create(name=name)
        return Response({'msg': '添加成功'})

    def get(self, request):
        # 获取工单列表
        workorder_list = Cate.objects.all()
        # 序列化
        workorder_ser = WorkOrderSerializer(workorder_list, many=True)
        z_list = xTree(workorder_ser.data)
        return Response(z_list)


class Small_WorkOrderView(APIView):
    def get(self, request):
        # 获取工单列表
        workorder_list = Cate.objects.all()
        # 序列化
        workorder_ser = WorkOrderSerializer(workorder_list, many=True)
        return Response(workorder_ser.data)


# MongoDB
from pymongo import MongoClient

# 连接MongoDB
client = MongoClient(host='127.0.0.1', port=27017)

# 获取MongoDB数据
class GetMongo(APIView):
    def get(self,request):
        # 进入数据库
        db = client.z_Cate
        # 集合
        zst = db.CateTemplate
        id=request.GET.get('id',None)
        wid=Cate.objects.filter(id=id).first()
        data = {}
        for i in zst.find({'wid': str(wid.pid)}):
            data = eval(i['name'])
        return Response(data)